﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PrinteMaster
{
    public partial class 特殊接口_会员扣费 : Form
    {
        public 特殊接口_会员扣费()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void button充值_Click(object sender, EventArgs e)
        {
            if (textBox充值账号.Text.Equals(""))
            {
                MessageBox.Show("请输入用户的QQ账号");
                return;
            }

            // 判断充值金额输入框是否处于已输入状态;
            if (textBox充值金额.Text.Equals(""))
            {
                MessageBox.Show("请输入需要扣费的金额，应该是非0正整数，单位：分");
                return;
            }

            // 判断是否为负数
            if (Convert.ToInt32(textBox充值金额.Text) <= 0)
            {
                MessageBox.Show("扣费金额只能为非0的正整数");
                return;
            }

   

            // 如果账号和金额都正确的被输入,提示确认充值
            ////////////////// 检测参数是否准确 //////////////
            string userName, userNumber, userPhoneNumber;
            if (DataBase.getInstance().isUserId(textBox充值账号.Text, out userNumber, out userName, out userPhoneNumber) != DataBase.Error.ok)
            {
                // 如果 会员简表内没有这个记录
                MessageBox.Show("没有该会员!", "提示");
                return;
            }
            double money;
            DataBase.Error er = DataBase.getInstance().getUserMoney(textBox充值账号.Text, out money);
            if(er != DataBase.Error.ok)
            {
                MessageBox.Show("数据库错误！=>"+er);
                return;
            }
            if(money < Convert.ToDouble(textBox充值金额.Text)/100)
            {
                MessageBox.Show("扣费金额大于用户现存余额，无法扣费！");
                return;
            }
            ////////////////// 提示充值者信息  //////////////
            string stringbuild = "\n 扣费账号:" + textBox充值账号.Text + "   |   扣费金额:" + (Convert.ToDouble(textBox充值金额.Text)/100).ToString("0.00") + " 元\n 扣费者姓名:" + userName;
            if (MessageBox.Show("确认扣费吗？" + stringbuild, "提示", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                // 进入充值
                // 填充结构体
                DataBase.PayInfo info = new DataBase.PayInfo();
                info.managerId = "0";
                info.managerName = "老板模式";
                info.money =Convert.ToDouble(textBox充值金额.Text)/-100;
                info.time = DateTime.Now;
                info.userId = textBox充值账号.Text;
                info.userName = userName;
                info.userNumber = userNumber;
                info.userPhoneNumber = userPhoneNumber;
                DataBase.Error error = DataBase.getInstance().addPayInfo(info);//建档
                error = DataBase.getInstance().subUserMoney(textBox充值账号.Text, Convert.ToDouble(textBox充值金额.Text)/100);
                if (error == DataBase.Error.ok)
                {
                    MessageBox.Show("扣费成功!");
                    this.Close();
                }
                else
                {
                    MessageBox.Show("扣费失败!原因=>"+error);
                    return;
                }
                string msg = string.Format("为客户[{0}]扣费{1}元", userName, (Convert.ToDouble(textBox充值金额.Text)/100).ToString("0.00"));
                Log.managerLog(msg,"0", "老板模式", "老板模式");
                return;
            }
        }

        private void textBox充值金额_KeyPress(object sender, KeyPressEventArgs e)
        {
            Function.inputInt(sender, e);
        }

        private void textBox充值账号_KeyPress(object sender, KeyPressEventArgs e)
        {
            Function.inputInt(sender, e);
        }
    }
}
